/*
 auto:besideyou  
 2020年3月4日15:39:38
 * 
 * 
 * */
window.onload = function(){	
	!(function(win,doc){
		
		function hzModal(){
			
			this.mask = doc.createElement('div');
			this.mask.setAttribute("class", 'hz-mask');
			
			var win = doc.body.clientWidth || document.documentElement.clientWidth ,wh = doc.body.clientHeight || document.documentElement.clientHeight;
			this.mask.style.width = win + 'px';
			this.mask.style.height = wh + 'px';
	
				//mask.addEventListener('click', this.close.bind(this),false);
			this.modal = doc.createElement('div');
			this.modal.setAttribute('class','hz-modal');
			var	body = doc.createElement('div');
				body.setAttribute('class','modal-body');
			var img = new Image(),
				p = doc.createElement("p"),
				footer = doc.createElement('div');
				footer.setAttribute('class','modal-foot');
			this.leftBtn = doc.createElement('span');
			this.leftBtn.setAttribute('class','btn cancel-btn');
			this.rightBtn = doc.createElement("span");	
			this.rightBtn.setAttribute('class','btn confirm-btn');
			this.createElement = function(opt){
				var opt = opt || {};
				opt.modalWidth ? modal.style.width = opt.modalWidth : null;
				p.innerText = opt.content || "这是内容";
				if(opt.showCancel){
				    this.leftBtn.innerText = opt.leftBtnText || "取消";	
				}
				this.rightBtn.innerText = opt.rightBtnText || '确认';	
				img.src= opt.icon;
				var _this = this;
				img.onload = function(){	
					img.setAttribute('class','icon');
					if(opt.showCancel){
						footer.appendChild(_this.leftBtn);
					}
					footer.appendChild(_this.rightBtn);
					body.appendChild(img);
					body.appendChild(p);
					body.appendChild(footer);
					_this.modal.appendChild(body);
					doc.body.appendChild(_this.modal);
					if(opt.showMask){
						doc.body.appendChild(_this.mask);
					}
					
				}
			
			}
		}
		hzModal.prototype = {
			
			success: function(opt){
				return new Promise((resolve,reject)=>{
					var data = opt || {};
					var successImg = '';
					data.icon = data.icon || successImg;
	
					this.createElement(data);
					var that = this;
					this.leftBtn.onclick = function(){
						reject('cancel');
						that.close(opt.showMask)
					}
					this.rightBtn.onclick = function(){
						resolve('confirm');
						that.close(opt.showMask);
					}
				})
			},
			error: function(opt){
				return new Promise((resolve,reject)=>{
					var data = opt || {};
					var errorImg = ''
					
					data.icon = data.icon || errorImg;
					this.createElement(data)
					var that = this;
					this.leftBtn.onclick = function(){
						reject('cancel');
						that.close(opt.showMask)
					}
					this.rightBtn.onclick = function(){
						resolve('confirm');
						that.close(opt.showMask);
					}
				})
			},
			warn: function(opt){
				return new Promise((resolve,reject)=>{
				var data = opt || {};
					var warnImg = ''
					
					data.icon = data.icon || warnImg;
					this.createElement(data)
					var that = this;
					this.leftBtn.onclick = function(){
						reject('reject');
						that.close(opt.showMask)
					}
					this.rightBtn.onclick = function(){
						resolve('confirm');
						that.close(opt.showMask);
					}
				})
			},
			close: function(show){
				if(show){
					doc.body.removeChild(this.mask);
				}
				
				doc.body.removeChild(this.modal);
			},
		
		}
		
		win.hzModal = new hzModal();
	})(window,document)
}